cc.Class({
    extends: cc.Component,

    properties: {
        motorBet: cc.Prefab,
        betBtns: [cc.Button],
        btnUndo: cc.Node,
    },

    /**
     * order: a,b,c,d,e
     * state: 0-1
     * role: bones
     * odds: str
     * rate: str
     */
    onLoad() {
        this.node.cascadeOpacity = false;
        this.node.find('BetCtrl').setLocalZOrder(100);
        this.btnUndo.on('click', () => {
            cc.Proxy.newburyingPoint(EventConfig.getEventId('撤销'));
            this.node.emit('BET_CANCEL');
        }, this);
    },

    loadMotors() {
        this.motors = [];
        var roles = ['红', '绿', '蓝', '紫', '黄'];
        for (var i = 0; i < roles.length; i++) {
            var obj = cc.instantiate(this.motorBet);
            obj.parent = this.node;
            obj.x = -440 + i * 220;
            obj.on('click', this.onBetClick, this);
            var motor = obj.getComponent('MotorBet');
            motor.init({
                order: i,
                role: roles[i],
                odds: 0,
                rate: 0
            });
            this.motors.push(motor);
        }
    },

    updateBetData() {
        var bikes = GLOBAL.gameState.bikes;
        for (var x = 0; x < bikes.length; x++) {
            var b = bikes[x];
            var i = b.id - 1;
            this.motors[i].updateData({
                odds: b.rate,
                rate: b.status
            });
        }

        var betting = GLOBAL.gameState.betting;
        for(var i in betting) {
            var btn = this.betBtns[i];
            btn.node.find('txt_count', cc.Label).string = betting[i];
        }
    },

    onBetClick(evt) {
        cc.Sound.playEffect('snd_bet');
        this.node.emit('BET_CLICK', evt.target.getComponent('MotorBet'));
        if(this.guidePrefab) {
            this.guidePrefab = null;
            for(var i in this.motors) this.motors[i].stopGuide();
        } 
    },

    showGuide() {
        cc.loadRes('GuidePoint', res => {
            this.guidePrefab = res;
            for(var i in this.motors) this.motors[i].playGuide(cc.instantiate(res));
        });
    },

    clearBet() {
        for(var i in this.motors) this.motors[i].motorOil.clearBet();
    },
    
    getBetBtn() {
        for(var i in this.betBtns) {
            var btn = this.betBtns[i];
            if(!btn.interactable) return btn.getComponent('BetRadio');
        }
    },
});
